﻿using Dapper;
using Domain.Entity.bs;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class PublicFileRepository : IPublicFileRepository
    {
        //连接字符串
        string con = "Data Source=121.37.93.71;Initial Catalog=CSSL_ZCTWMS;User ID=sa;Password=geili@2025;Encrypt=False";

        //添加附件
        public async Task<int> AddFile(string? type, string? code, string? file,string? title)
        {
            using(SqlConnection sqlConnection=new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"insert into [dbo].[bs_public_file](business_type, business_code, title, file_name)
                                values('{type}','{code}','{title}','{file}')";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }


        //添加附件
        public async Task<int> AddFile(PublicFile publicFile)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"insert into [dbo].[bs_public_file](business_type, business_code, title, file_name)
                                values('{publicFile.business_type}','{publicFile.business_code}','{publicFile.title}','{publicFile.file_name}')";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }


        //删除公共附件
        public async Task<int> DeleteFile(string? type, string? code)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"delete from [dbo].[bs_public_file] where business_type='{type}' and business_code='{code}'";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }


        //根据业务类型和对应编号查询附件
        public async Task<List<PublicFile>> GetPublicFileBy(string? type, string? code)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"select * from [dbo].[bs_public_file] where business_type='{type}' and business_code='{code}'";
                return sqlConnection.Query<PublicFile>(sql).ToList();
            }
        }
    }
}
